<?php 
class DocumentsController extends AppController {
	var $name = 'Documents';
	var $helpers = array('Html', 'Form');	

/*
 *  anketa выводит заполненную данными анкету абитуриента
 */

	function anketa(){
		$id = $this->params['named']['abiturient'];
		$this->loadModel('Abiturient');
		$this->Abiturient->recursive = 1;
		$abiturient = $this->Abiturient->find('first', array('conditions' => array('Abiturient.id' => $id)));
		$this->set('abiturient', $abiturient);
		$selectedprofessions = $this->Abiturient->Selectedprofession->find('all', array('conditions' => array('Selectedprofession.abiturient_id' => $id), 'order' => array('Selectedprofession.id')));
		$this->set('selectedprofessions', $selectedprofessions);
		$this->loadModel('Facultet');
		$professions = $this->Facultet->Profession->find('all');
		$this->set('professions', $professions);
		$this->loadModel('Learningform');
		$learningforms = $this->Learningform->find('all');
		$this-> set('learningforms',$learningforms);
		$this->LoadModel('Learningtype');
		$learningtypes=$this->Learningtype->find('all');
		$this->set('learningtypes',$learningtypes);
		$this->LoadModel('Schoolflanguage');
		$schoolflanguages=$this->Schoolflanguage->find('list');
		$this->set('schoolflanguages',$schoolflanguages);
		$this->loadModel('Subject');
		$subjects = $this->Subject->find('list');
		$this->set('subjects', $subjects);
		$this->loadModel('Resulttype');
		$resulttypes = $this->Resulttype->find('list');
		$this->set('resulttypes', $resulttypes);
		// убираем debug
		//Configure::write('debug', 0);
		// выводим в пустой шаблон 
		$this->render('/','blank','anketa');
	}
/*
 * выводит опись документов переданных абитуриентом
 */	
	function abiturientdocs() {
		$id = $this->params['named']['abiturient'];
		$this->loadModel('Abiturient');
		$this->Abiturient->recursive = 1;
		$abiturient = $this->Abiturient->find('first', array('conditions' => array('Abiturient.id' => $id)));
		$this->set('abiturient', $abiturient);
		$selectedprofessions = $this->Abiturient->Selectedprofession->find('all', array('conditions' => array('Selectedprofession.abiturient_id' => $id), 'order' => array('Selectedprofession.id')));
		$this->set('selectedprofessions', $selectedprofessions);
		$this->loadModel('Facultet');
		$professions = $this->Facultet->Profession->find('all');
		$this->set('professions', $professions);
		$this->loadModel('Educationdoctype');
		$educationdoctypes = $this->Educationdoctype->find('list');
		$this->set('educationdoctypes',$educationdoctypes);

		// убираем debug
		//Configure::write('debug', 0);
		// выводим в пустой шаблон 
	  $this->render('/','blank','abiturientdocs');
	}
/*
 * Выводит количество поданных заявлений
 */
	function totalabiturientscount() {
		// выбираем все факультеты
		$this->loadModel('Facultet'); 
		$facultets = $this->Facultet->find('all', array('recursive' => -1));
		// для поиска по форме обучения
		$this->loadModel('Learningform');
		$learningforms = $this->Learningform->find('all', array('recursive' => -1));	
		// для поиска по типу обучения
		$this->loadModel('Learningtype');
		$learningtypes = $this->Learningtype->find('all', array('recursive' => -1));	
		// подключаем модель абитуриент
		$this->loadModel('Abiturient');	
		
		// подключаем модель Profession
		$this->loadModel('Profession');
		// обрабатываем все факультеты вибирая заявления абитуриентов
		
		$facultets_total = array();
		
		foreach ($facultets as $facultet):
			$facultet_id = $facultet['Facultet']['id'];	
			// выбираем все специальности на факультете
			
			$this->Profession->recursive = -1;
			$professions = $this->Profession->find('all', array('conditions' => array('Profession.facultet_id' => $facultet_id)));
			$profession_ids = array(); // содержит id всех специальностей факультета
			foreach ($professions as $profession):
				array_push($profession_ids, $profession['Profession']['id']);
			endforeach;

			// выбрали все id абитуриентов по специальности по всем формам по всем типам обучения
			
			$facultet_selectedprofessions = array(); // содержит все записи из Selectedprofession по данному факультету
			foreach ($profession_ids as $profession_id):
				foreach($learningforms as $learningform):
					foreach($learningtypes as $learningtype):
						$selectedprofessionscount = $this->Abiturient->Selectedprofession->find('count', array('conditions' => array(
						'Selectedprofession.profession_id' => $profession_id,
						'Selectedprofession.learningform_id' => $learningform['Learningform']['id'],
						'Selectedprofession.learningtype_id' => $learningtype['Learningtype']['id']),
						'recursive' => -1,));
						// если массив из Selectedprofession не пустой пишем в большой массив
						if ($selectedprofessionscount != NULL){ 
							$selectedprofession = array('facultet_id' => $facultet_id,  'profession_id' => $profession_id, 'learningform_id' => $learningform['Learningform']['id'], 'learningtype_id' => $learningtype['Learningtype']['id'], 'count' => $selectedprofessionscount);
							
						array_push($facultet_selectedprofessions, $selectedprofession);
						}	
					endforeach;
				endforeach;
			endforeach;
			array_push ($facultets_total, $facultet_selectedprofessions);
		endforeach; // ($facultets as $facultet)
		$this->set('facultets_total', $facultets_total);
		/*
		$this->loadModel('Facultet');
		$professions = $this->Facultet->Profession->find('all');
		$this->set('professions', $professions);
		$this->LoadModel('Learningtype');
		$learningtypes=$this->Learningtype->find('all');
		$this->loadModel('Selectedprofession');
		$professionsb_count = array();
		$professionsvb_count = array();
		
		foreach ($professions as $profession):
			$professionb_count = $this->Selectedprofession->find('count', array('conditions' => array('Selectedprofession.profession_id' => $profession['Profession']['id'], 'Selectedprofession.learningtype_id' => 1)));
			$professionvb_count = $this->Selectedprofession->find('count', array('conditions' => array('Selectedprofession.profession_id' => $profession['Profession']['id'], 'Selectedprofession.learningtype_id' => 2)));
			array_push($professionsb_count, $professionb_count);
			array_push($professionsvb_count, $professionvb_count);
		endforeach;
		
		$this->set('professionsb_count', $professionsb_count); 
		
		$this->set('professionsvb_count', $professionsvb_count);
		*/
		$this->render('/','blank','totalabiturientscount');
	}
/*
 * выводит пофамильно списки абитуриетов по факультетам
 */
	function facultetsabiturientslist() {
		//Configure::write('debug', 0);
		// получили id факультета
		//$facultet_id = $this->data['Document']['facultet_id'];
		$this->loadModel('Facultet'); 
		$facultets = $this->Facultet->find('all', array('recursive' => -1));
		
		$facultetslist = $this->Facultet->find('list');
		// для вывода названия специальности
		$this->loadModel('Profession');
		$viewprofessions = $this->Profession->find('list');
		$this->set('professions', $viewprofessions);

		// для вывода названия формы обучения
		$this->loadModel('Learningform');
		$viewlearningforms = $this->Learningform->find('list');
		$this->set('learningforms', $viewlearningforms);
		// для поиска по форме обучения
		$learningforms = $this->Learningform->find('all', array('recursive' => -1));
		// для выовда названия типа обучения
		$this->loadModel('Learningtype');
		$viewlearningtypes = $this->Learningtype->find('list');
		$this->set('learningtypes', $viewlearningtypes);
		// для поиска по типу обучения
		$learningtypes = $this->Learningtype->find('all', array('recursive' => -1));
		
		//debug($facultets);
		// подключаем модель абитуриент
		$this->loadModel('Abiturient');	
		
		// обрабатываем все факультеты вибирая заявления абитуриентов
		
		$facultets_total = array();
		
		foreach ($facultets as $facultet):
			$facultet_id = $facultet['Facultet']['id'];
			// выбираем все специальности на факультете
			$this->Profession->recursive = -1;
			$professions = $this->Profession->find('all', array('conditions' => array('Profession.facultet_id' => $facultet_id)));
			$profession_ids = array(); // содержит id всех специальностей факультета
			foreach ($professions as $profession):
			array_push($profession_ids, $profession['Profession']['id']);
			endforeach;		
				
			// выбрали все id абитуриентов по специальности по всем формам по всем типам обучения
			
			$facultet_selectedprofessions = array(); // содержит все записи из Selectedprofession по данному факультету
			foreach ($profession_ids as $profession_id):
				foreach($learningforms as $learningform):
					foreach($learningtypes as $learningtype):
						$selectedprofessions = $this->Abiturient->Selectedprofession->find('all', array('conditions' => array(
						'Selectedprofession.profession_id' => $profession_id,
						'Selectedprofession.learningform_id' => $learningform['Learningform']['id'],
						'Selectedprofession.learningtype_id' => $learningtype['Learningtype']['id']),
						'recursive' => -1,));
						// если массив из Selectedprofession не пустой пишем в большой массив
						if ($selectedprofessions != NULL){ 
						array_push($facultet_selectedprofessions, $selectedprofessions);
						}	
					endforeach;
				endforeach;
			endforeach;

			// 	формируем массив id абитуриентов 	
			$sorted_selectedprofessions_abiturients_ids = array();
			foreach ($facultet_selectedprofessions as $professions):
				$i = true;
				$description_arr = array(); // содержит id специальности id формы и id типа обучения
				$abiturient_ids_arr = array();// содержит id абитуриентов по текущей id специальности id формы и id типа обучения
				foreach($professions as $selectedprofession):
					if ($i){
						$description_arr = array( 'profession_id' => $selectedprofession['Selectedprofession']['profession_id'], 'learningform_id' => $selectedprofession['Selectedprofession']['learningform_id'], 'learningtype_id' => $selectedprofession['Selectedprofession']['learningtype_id']);			
						$i = false;
					}
					array_push($abiturient_ids_arr, $selectedprofession['Selectedprofession']['abiturient_id']);
				endforeach;
				/*
				 * собираем массив вида	profession_id
				 * 						learnigform_id
				 * 						learningtype_id
				 * 						[0] - array() - id всех абитуриентов по текущей id специальности id формы и id типа обучения
				 * 	в description_arr
				 */
				
				array_push($description_arr, $abiturient_ids_arr);
				array_push($sorted_selectedprofessions_abiturients_ids, $description_arr);	
			endforeach;			

			//формируем список абитуриентов отсортированный по фамилии 
			$facultet_total = array();
			foreach ($sorted_selectedprofessions_abiturients_ids as $element):
				$element_abiturients_ids = array();
				foreach ($element[0] as $abiturient_id):
					array_push($element_abiturients_ids, $abiturient_id);
				endforeach;
				$abiturients = $this->Abiturient->find('all', array ('conditions' => array(
						'Abiturient.id' => $element_abiturients_ids), 
						'recursive' => -1,
						'order' => 'Abiturient.lastname ASC',
						'fields' => array ('Abiturient.id', 'Abiturient.lastname', 'Abiturient.firstname', 'Abiturient.surname')
														)); 
			// формируем массив с ФИО абитуриентов
			$facultet_total_element = array ('facultet_id' => $facultet_id, 'profession_id' => $element ['profession_id'], 'learningform_id' => $element ['learningform_id'], 'learningtype_id' => $element ['learningtype_id'], 'abiturients' => $abiturients) ;
			array_push($facultet_total, $facultet_total_element);
			endforeach;			
			// заносим id факультета чтобы потом его вывести
			//array_push ($facultets_total, $facultet);
			array_push ($facultets_total, $facultet_total);
		endforeach; // $facultets

	$this->set ('facultets_total', $facultets_total);
	$this->set('facultetslist', $facultetslist);
	// подключаем шаблон

	$this->render('/','blank','facultetsabiturientslist');		
	}
	
	function abiturientschecklist  () {
		 Configure::write('debug', 0);
		// подгружаем модель Abiturient
		
		$this->loadModel('Subject');
		$subjects = $this->Subject->find('list');
		
		$this->loadModel('Profession');
		$professions = $this->Profession->find('list');
		$facultets = $this->Profession->Facultet->find('list');
		
		$this->loadModel('Region');
		$regions = $this->Region->find('list');
		
		$this->loadModel('Privilege');
		$privileges = $this->Privilege->find('list');
		
		$this->loadModel('Prioritet');
		$prioritets = $this->Prioritet->find('list');

		$this->loadModel('Learningform');
		$learningforms = $this->Learningform->find('list');
		
		$this->loadModel('Learningtype');
		$learningtypes = $this->Learningtype->find('list');
		
		$this->loadModel('Tgintakeorg');
		$tgintakeorgs = $this->Tgintakeorg->find('list');		
		
		$this->loadModel('Abiturient');
			
	// 	$abiturients = $this->Abiturient->find('all', array('conditions' => array('Abiturient.enrolled' => '0', 'Abiturient.canceled' => '0'), 'order' => array('Abiturient.id'), 'fields' => array('id', 'lastname', 'firstname', 'surname', 'originaleducationdoc')));
		$abiturients = $this->Abiturient->find('all', array('conditions' => array('Abiturient.canceled' => '0'), 'order' => array('Abiturient.id'), 'fields' => array('id', 'lastname', 'firstname', 'surname', 'originaleducationdoc')));	
		$this->set ('abiturients', $abiturients);
		$abiturients_selectedprofessions = array();
		foreach ($abiturients as $abiturient):
		$abiturient_selectedprofessions = $this->Abiturient->Selectedprofession->find('all', array('conditions' => array('Selectedprofession.abiturient_id' => $abiturient['Abiturient']['id']), 'order' => array('Selectedprofession.id'), 'recursive' => -1));
		array_push($abiturients_selectedprofessions, $abiturient_selectedprofessions);
		endforeach;
		$this->set('abiturients_selectedprofessions', $abiturients_selectedprofessions);
		$this->set(compact('subjects', 'professions', 'regions', 'facultets', 'privileges', 'prioritets', 'tgintakeorgs',  'learningforms', 'learningtypes'));
		$this->render('/','blank','abiturientschecklist');
		
	}
	/*
	* выводит csv файл для отправаки в ФБС
	*/
	function fbsfile () {
		Configure::write('debug', 0);
		$this->loadModel('Abiturient');
		if ($this->data['Document']['abiturient_id']){
			$abiturients = $this->Abiturient->find('all', array('conditions' => array('Abiturient.id >' => $this->data['Document']['abiturient_id']),
																		 'fields' => array('Abiturient.firstname', 
																							'Abiturient.surname', 
																							'Abiturient.lastname', 
																							'Abiturient.passport_ser', 
																							'Abiturient.passport_num')));			
		}
		else {
			$abiturients = $this->Abiturient->find('all', array('fields' => array('Abiturient.firstname',
																							'Abiturient.surname', 
																							'Abiturient.lastname', 
																							'Abiturient.passport_ser', 
																							'Abiturient.passport_num')));
		}
		$this->set('abiturients', $abiturients);
		// подключаем шаблон
		$this->render('/','blank','fbsfile');
	}
	/*
	* выводит списки целивиков по факультетам отсортированные по убыванию баллов
	*/
	function tgintakeinfos () {
		Configure::write('debug', 0);
		
		$this->loadModel('Learningform');
		$learningforms = $this->Learningform->find('list');
		$this->set('learningforms',$learningforms);
		$this->loadModel('Learningtype');
		$learningtypes = $this->Learningtype->find('list');
		$this->set('learningtypes',$learningtypes);

		$this->loadModel('Subject');
		$subjects = $this->Subject->find('list');
		$this->set('subjects', $subjects);		
		
		$this->loadModel('Tgintakeorg');
		$tgintakeorgs = $this->Tgintakeorg->find('list');
		$this->set('tgintakeorgs', $tgintakeorgs);

		$this->loadModel('Region');
		$regions = $this->Region->find('list');
		$this->set('regions', $regions);
		
		$this->loadModel('Facultet');
		$facultets = $this->Facultet->find('all', array('recursive' => 1,));
		
		$this->loadModel('Profession');
		$professions = $this->Profession->find('all');
		
		$professionslist =  $this->Profession->find('list');
		$this->set('professions', $professionslist);
		
		$professions_subjects = array();
		
		$tgprofessions = array();
 		foreach ($professions as $profession): 
 		
		$tgprofession = array('Profession' => $profession['Profession'], 'Subject'=> $profession['Subject'], 'Targetingintake' => $profession['Targetingintake']);
		array_push($tgprofessions, $tgprofession);
		$profession_subjects = array();
			foreach ($profession['Subject'] as $subject):
				array_push($profession_subjects, $subject['id']);
			endforeach;
			array_push($professions_subjects, array('profession_id' => $profession['Profession']['id'], 'subject_ids' => $profession_subjects));
		endforeach; 
		
		// обрабатываем id абитуриентов целивиков
		
//		debug($tgprofessions);
	//	debug($professions_subjects);
		
		$this->set('professions_subjects', $professions_subjects);
		
		$allprofessions_abiturients_ids = array();
		foreach ($tgprofessions as $tgprofession):
		
			$profession_tgintakes = $tgprofession['Targetingintake'];
			$abiturients_ids = array();
			foreach ($profession_tgintakes as $profession_tgintake):
				array_push($abiturients_ids, $profession_tgintake['abiturient_id']);
			endforeach;
			array_push($allprofessions_abiturients_ids, array('profession_id' =>$tgprofession['Profession']['id'], 'abiturients_ids' => $abiturients_ids));
		endforeach;
		
		$this->loadModel('Abiturient');
		
		$all_abiturients = array();
		
		$all_abiturientsinfo = array();
		
		foreach($allprofessions_abiturients_ids as $profession_abiturients_ids):
		
		$subjects_ids = $this->Profession->SubjectsProfession->find('all', array('conditions' => array('SubjectsProfession.profession_id' => $profession_abiturients_ids['profession_id'])));
		$this->set('subjects_ids', $subjects_ids);
		
			$abiturients = $this->Abiturient->find('all', array('conditions' => array('Abiturient.id' => $profession_abiturients_ids['abiturients_ids'],
																								'Abiturient.prepared' => 1,
																								'Abiturient.approved' => 1,
																								'Abiturient.canceled' => 0,
																								'Abiturient.enrolled' => 0,
			),
									'recursive' => 1,
									'fields' => array ('Abiturient.id', 
														'Abiturient.lastname', 
														'Abiturient.firstname', 
														'Abiturient.surname', 
														'Abiturient.originaleducationdoc',
														'phone',
														'mobile'
			)
			));
			
			// засовываем информацию об абитуриентах по всем специальностям в один массив
			foreach ($abiturients as $abiturient):
				array_push ($all_abiturientsinfo, $abiturient);
			endforeach;
			// считаем сумму баллов по специальности
			$score_total_abiturients = array(); // к данным абитуриента добавлен елемент Score_total
			foreach ($abiturients as $abiturient):
			// считаем  сумму балов по специальности
			$score_total = 0;
				foreach ($subjects_ids as $subject_id):
					foreach ($abiturient['Result'] as $result):
					if ($subject_id['SubjectsProfession']['subject_id'] == $result['subject_id']){
						$score_total += $result['score'];
					}
					endforeach;
				endforeach;
			$abiturient['Score_total'] = $score_total;
			array_push($score_total_abiturients, $abiturient);
			endforeach;
			// начинаем разбирать абитуриентов в  массив с сортровкой по рейтингу
			$arr_region_scoretotal_ids = array();
			foreach ($score_total_abiturients as $abiturient):
				$arr_region_scoretotal_ids[$abiturient['Abiturient']['id']] = $abiturient['Score_total'];
			endforeach;
			// сортируем массив целивиков по убыванию	Score_total
			arsort($arr_region_scoretotal_ids);
			// массив индексов абитуриентов льготников
		
			array_push($all_abiturients, array('profession_id' => $profession_abiturients_ids['profession_id'], 'sorted_score_abiturients_ids' => $arr_region_scoretotal_ids));

		endforeach;
		// debug ($all_abiturientsinfo);
		$this->set('all_abiturients', $all_abiturients);
		$this->set('all_abiturientsinfo', $all_abiturientsinfo);
		$this->render('/','blank','tgintakeinfos');
	}
	/*
	* Выводит рейтинг по факультету специальности форме и типу обучения
	*/
	function facultetrating() {
		$facultet_id = $this->data['Document']['facultet_id'];
		$profession_id = $this->data['Document']['profession_id'];
		$learningform_id = $this->data['Document']['learningform_id'];
		$learningtype_id = $this->data['Document']['learningtype_id'];
		
		$this->loadModel('Profession');
		$profession = $this->Profession->find('all', array('conditions' => array('Profession.id' => $profession_id)));		

		// получаем id предметов по специальности
		$subjects_ids = $this->Profession->SubjectsProfession->find('all', array('conditions' => array('SubjectsProfession.profession_id' => $profession_id)));

		$profession_subjects = array();
		foreach ($subjects_ids as $subject):
		array_push($profession_subjects, $subject['SubjectsProfession']['subject_id']);
		endforeach;		

		$this->set('subjects_ids', $profession_subjects);
		
			
		$this->loadModel('Targetingintake');
		$this->loadModel('Abiturient');
		
		$this->loadModel('Region');
		$regions = $this->Region->find('list');
		$this->set('regions', $regions);
		
		$this->loadModel('Subject');
		$subjects = $this->Subject->find('list');
		$this->set('subjects', $subjects);
		
		$this->loadModel('Tgintakeorg');
		$tgintakeorgs = $this->Tgintakeorg->find('all' , array('recursive' => -1));	
		$tgintakeorgslist = $this->Tgintakeorg->find('list');
		$this->set('tgintakeorgslist', $tgintakeorgslist);

			// массив id фабитиентов исключаемых из дальнейшего поиска
			$excluded_ids = array();
			// id абитуриентов сгрупированных по огранизациям выдавшим целевое отсортированным по баллам
			$celevoe_tgintakeorgs_abiturients = array();
			foreach ($tgintakeorgs as $tgintakeorg):
				
				$tgabiturients = $this->Targetingintake->find('all', array('conditions' => array('Targetingintake.profession_id' => $profession_id,
					                 															     'Targetingintake.learningform_id' => $learningform_id, 
					                 															     'Targetingintake.learningtype_id' => $learningtype_id,
																									 'Targetingintake.tgintakeorg_id' => $tgintakeorg['Tgintakeorg']['id']),
																'recursive' => -1
																));
				$abiturients_ids = array();
				
				foreach ($tgabiturients as $abiturient):
						array_push($abiturients_ids, $abiturient['Targetingintake']['abiturient_id']);
						array_push($excluded_ids, $abiturient['Targetingintake']['abiturient_id']);
				endforeach;

				// выбираем инфо по абитуриентам

				$abiturients = $this->Abiturient->find('all', array('conditions' => array('Abiturient.id' => $abiturients_ids,
																												'Abiturient.prepared' => 1,
																												'Abiturient.approved' => 1,
																												'Abiturient.canceled' => 0,
																											//	'Abiturient.enrolled' => 0,
																												),
													'recursive' => 1,
													'fields' => array ('Abiturient.id', 
																		'Abiturient.lastname', 
																		'Abiturient.firstname', 
																		'Abiturient.surname', 
																		'Abiturient.originaleducationdoc',)));		

				// считаем сумму баллов по специальности
				$score_total_abiturients = array(); // к данным абитуриента добавлен елемент Score_total
				foreach ($abiturients as $abiturient):
					// считаем  сумму балов по специальности
					$score_total = 0;
					foreach ($subjects_ids as $subject_id):
						foreach ($abiturient['Result'] as $result):
						if ($subject_id['SubjectsProfession']['subject_id'] == $result['subject_id']){
							$score_total += $result['score'];
						}
						endforeach;
					endforeach;
					$abiturient['Score_total'] = $score_total;
					array_push($score_total_abiturients, $abiturient);
				endforeach;
				// начинаем разбирать абитуриентов в  массив с сортровкой по рейтингу
				$arr_region_scoretotal_ids = array();
				foreach ($score_total_abiturients as $abiturient):
				$arr_region_scoretotal_ids[$abiturient['Abiturient']['id']] = $abiturient['Score_total'];
				endforeach;
				// сортируем массив целивиков по убыванию	Score_total
				arsort($arr_region_scoretotal_ids);		

				array_push($celevoe_tgintakeorgs_abiturients, array('tgintakeorg_id' =>  $tgintakeorg['Tgintakeorg']['id'], 'sortedabiturients_ids' => $arr_region_scoretotal_ids));
			endforeach; // tgintakeorgs

						$this->set('celevoe_tgintakeorgs_abiturients',$celevoe_tgintakeorgs_abiturients);
			
			
			/*
			 * вы вибираем льгоников
			 */
			$this->loadModel('Privilege');
			$this->loadModel('Selectedprofession');
						
			$privileges = $this->Privilege->find('all', array('recursive' => -1));
			$privelegeslist = $this->Privilege->find('list');
			$this->set('privilegeslist',$privelegeslist);
			
			$privileges_abiturients = array();
			foreach ($privileges as $privilege):
			
				$privilege_abiturients = array();
			
				$id_not_in_ids = array('NOT' => array('Selectedprofession.abiturient_id' => $excluded_ids));
				$selectedprofession_abiturients_ids = $this->Selectedprofession->find('all', array('conditions' => array( $id_not_in_ids,
																										 'Selectedprofession.profession_id' => $profession_id,
						                 															     'Selectedprofession.learningform_id' => $learningform_id, 
						                 															     'Selectedprofession.learningtype_id' => $learningtype_id,),
																	'recursive' => -1,
																	'fields' => array('Selectedprofession.abiturient_id')
																	));
				$abiturients_ids = array();
				foreach ($selectedprofession_abiturients_ids as $abiturient):
							array_push($abiturients_ids, $abiturient['Selectedprofession']['abiturient_id']);
				endforeach;
				
				$this->loadModel('AbiturientsPrivilege');
				$selectedprofession_privilege_ids = $this->AbiturientsPrivilege->find('all', array('conditions' => array('AbiturientsPrivilege.abiturient_id' => $abiturients_ids, 
																															'AbiturientsPrivilege.privilege_id' => $privilege['Privilege']['id']),
																						'recursive' => -1,
																						));
				// debug($selectedprofession_privileges_ids);
				$abiturients_ids = array();			
				foreach ($selectedprofession_privilege_ids as $abiturient_id):
					array_push($abiturients_ids, $abiturient_id['AbiturientsPrivilege']['abiturient_id']);		
					array_push($excluded_ids, $abiturient_id['AbiturientsPrivilege']['abiturient_id']);					
				endforeach;
				

				// выбираем инфо по абитуриентам
				
				$abiturients = $this->Abiturient->find('all', array('conditions' => array('Abiturient.id' => $abiturients_ids,
																																'Abiturient.prepared' => 1,
																																'Abiturient.approved' => 1,
																																'Abiturient.canceled' => 0,
																																//'Abiturient.enrolled' => 0,
																																),
																	'recursive' => 1,
																	'fields' => array ('Abiturient.id', 
																						'Abiturient.lastname', 
																						'Abiturient.firstname', 
																						'Abiturient.surname', 
																						'Abiturient.originaleducationdoc')));		
				
				// считаем сумму баллов по специальности
				$score_total_abiturients = array(); // к данным абитуриента добавлен елемент Score_total
				foreach ($abiturients as $abiturient):
				// считаем  сумму балов по специальности
				$score_total = 0;
				foreach ($subjects_ids as $subject_id):
				foreach ($abiturient['Result'] as $result):
				if ($subject_id['SubjectsProfession']['subject_id'] == $result['subject_id']){
					$score_total += $result['score'];
				}
				endforeach;
				endforeach;
				$abiturient['Score_total'] = $score_total;
				array_push($score_total_abiturients, $abiturient);
				endforeach;
				// начинаем разбирать абитуриентов в  массив с сортровкой по рейтингу
				$arr_privilege_scoretotal_ids = array();
				foreach ($score_total_abiturients as $abiturient):
				$arr_privilege_scoretotal_ids[$abiturient['Abiturient']['id']] = $abiturient['Score_total'];
				endforeach;
				// сортируем массив целивиков по убыванию	Score_total
				arsort($arr_privilege_scoretotal_ids);
				
				array_push($privileges_abiturients, array('privilege_id' =>  $privilege['Privilege']['id'], 'sortedabiturients_ids' => $arr_privilege_scoretotal_ids));				
				
			endforeach;	
			
			// debug($privileges_abiturients);
			$this->set('privileges_abiturients',$privileges_abiturients);
			
			/*
			 * выбираем абитуриентов для простого рейтинга
			 */
			
			$plainrating_abiturients = array ();
			$id_not_in_ids = array('NOT' => array('Selectedprofession.abiturient_id' => $excluded_ids));
			$selectedprofession_abiturients_ids = $this->Selectedprofession->find('all', array('conditions' => array( $id_not_in_ids,
																													 'Selectedprofession.profession_id' => $profession_id,
									                 															     'Selectedprofession.learningform_id' => $learningform_id, 
									                 															     'Selectedprofession.learningtype_id' => $learningtype_id,),
																				'recursive' => -1,
																				'fields' => array('Selectedprofession.abiturient_id')
			));
			$abiturients_ids = array();
			foreach ($selectedprofession_abiturients_ids as $abiturient):
			array_push($abiturients_ids, $abiturient['Selectedprofession']['abiturient_id']);
			array_push($excluded_ids, $abiturient['Selectedprofession']['abiturient_id']);			
			endforeach;
						
			// выбираем инфо по абитуриентам
			
			$abiturients = $this->Abiturient->find('all', array('conditions' => array('Abiturient.id' => $abiturients_ids,
																										'Abiturient.prepared' => 1,
																										'Abiturient.approved' => 1,
																										'Abiturient.canceled' => 0,
																										//'Abiturient.enrolled' => 0,
																										),
																				'recursive' => 1,
																				'fields' => array ('Abiturient.id', 
																									'Abiturient.lastname', 
																									'Abiturient.firstname', 
																									'Abiturient.surname', 
																									'Abiturient.originaleducationdoc',)));		
			
			// считаем сумму баллов по специальности
			$score_total_abiturients = array(); // к данным абитуриента добавлен елемент Score_total
			foreach ($abiturients as $abiturient):
			// считаем  сумму балов по специальности
			$score_total = 0;
			foreach ($subjects_ids as $subject_id):
			foreach ($abiturient['Result'] as $result):
			if ($subject_id['SubjectsProfession']['subject_id'] == $result['subject_id']){
				$score_total += $result['score'];
			}
			endforeach;
			endforeach;
			$abiturient['Score_total'] = $score_total;
			array_push($score_total_abiturients, $abiturient);
			endforeach;
			
			// начинаем разбирать абитуриентов в  массив с сортровкой по рейтингу
			$plainrating_abiturients = array();
			foreach ($score_total_abiturients as $abiturient):
			$plainrating_abiturients[$abiturient['Abiturient']['id']] = $abiturient['Score_total'];
			endforeach;
			// сортируем массив целивиков по убыванию	Score_total
			arsort($plainrating_abiturients);
			
			$this->set('plainrating_abiturients',$plainrating_abiturients);
			
			$abiturients = $this->Abiturient->find('all', array('conditions' => array('Abiturient.id' => $excluded_ids,
																																'Abiturient.prepared' => 1,
																																'Abiturient.approved' => 1,
																																'Abiturient.canceled' => 0,
																																//'Abiturient.enrolled' => 0,
																																),
																	'recursive' => 1,
																	'fields' => array ('Abiturient.id', 
																						'Abiturient.lastname', 
																						'Abiturient.firstname', 
																						'Abiturient.surname', 
																						'Abiturient.originaleducationdoc',
																						'Abiturient.region_id')));
			$this->set('abiturients', $abiturients)	;		
		$this->render('/','blank','facultetrating');
	}	
	/*
	 * journal вывожит журнал регистрации абитуриентов
	 */
	function journal() {
		Configure::write('debug', 0);
		$this->loadModel('Abiturient');
		
		$abiturients = $this->Abiturient->find('all', array(		'recursive' => 1,
																	'order' => 'Abiturient.id', 	
																	'fields' => array ('Abiturient.id', 
																						'Abiturient.lastname', 
																						'Abiturient.firstname', 
																						'Abiturient.surname', 
																						'Abiturient.created',
																						'Abiturient.modified',																						
																						'Abiturient.canceled',
																						'Abiturient.educationdoctype_id',
																						'Abiturient.educationdocdata',
																						'Abiturient.adress')));
		$this->set('abiturients', $abiturients);
		$this->render('/','blank','journal');		
	}
	/*
	 * справка для министерства здравсоц развития
	 */
	function megaspravka () {
	//	Configure::write('debug', 0);
		$this->loadModel('Learningform');
		$learningforms = $this->Learningform->find('all', array('recursive' => -1));
		$this->loadModel('Learningtype');
		$learningtypes = $this->Learningtype->find('all', array('recursive' => -1));	
		
		$listlearningforms = $this->Learningform->find('list');
		$this->set('listlearningforms', $listlearningforms);
		
		$listlearningtypes = $this->Learningtype->find('list');
		$this->set('listlearningtypes', $listlearningtypes);
		
		$this->loadModel('Enrolledstudent');
		$this->loadModel('Result');
		$this->loadModel('Abiturient');
		
		$this->loadModel('Facultet');
		$this->loadModel('Profession');
		
		$listfacultets = $this->Facultet->find('list');
		$this->set(compact('listfacultets'));
		
		$listprofessions = $this->Profession->find('list');
		$this->set(compact('listprofessions'));
		
		$facultets = $this->Facultet->find('all', array('recursive' => -1));
		//debug ($learningtypes);
		$spravka = array();
		foreach ($facultets as $facultet):
			$professions = $this->Profession->find('all', array('conditions' => array('Profession.facultet_id' => $facultet['Facultet']['id']), 'recursive' => -1));
			foreach ($professions as $profession):
				foreach ($learningforms as $learningform):
					foreach ($learningtypes as $learningtype):
						$students = $this->Enrolledstudent->find ('all', array('conditions' => 
								array('Enrolledstudent.profession_id' => $profession['Profession']['id'], 
									  'Enrolledstudent.learningform_id' => $learningform['Learningform']['id'],
									  'Enrolledstudent.learningtype_id' => $learningtype['Learningtype']['id']
									), 'recursive' => -1));
					//debug ($students);
					$enrolled_ids = array();
						// получаем id зачисленных на факультет
						foreach ($students as $student):
							array_push($enrolled_ids, $student['Enrolledstudent']['abiturient_id']);
						endforeach;
						// считаем сколько заевлений подано по критериям
						$abiturient_ids = array();
						$profession_abiturient_ids = $this->Profession->Selectedprofession->find('all', array('conditions' => 
											array('Selectedprofession.profession_id'  => $profession['Profession']['id'],
												  'Selectedprofession.learningform_id' => $learningform['Learningform']['id'],
												  'Selectedprofession.learningtype_id' => $learningtype['Learningtype']['id']
											), 'recursive' => -1, 'fields' => array('Selectedprofession.abiturient_id')));
						foreach ($profession_abiturient_ids as $profession_abiturient_id):
							array_push($abiturient_ids, $profession_abiturient_id['Selectedprofession']['abiturient_id']);
						endforeach;
						
						// обнуляем счетчики
							$ege = 0;
							$fct = 0;
							$high_edu = 0;
							$middle_edu = 0;
							
							$p_ege = 0;
							$p_fct = 0;
							$p_high_edu = 0;
							$p_middle_edu = 0;							
	
						//
						 // проверяем зачисленных
						 //
						foreach ($enrolled_ids as $enrolled_id):
							$results = $this->Result->find('all', array('conditions' => array('Result.abiturient_id' => $enrolled_id), 'recursive' => -1, ));
							// проверяем тип оценки
							$is_ege = true;	
							foreach ($results as $result):
								if ($result['Result']['resulttype_id'] != 1){
									$is_ege = false;
									break; 
								}
							endforeach;
							// проверяем все оценки
							if ($is_ege){
								$ege++;
							} else {
								$fct++;
							}					
						endforeach;
						$doctypes = array(5,6);
						$high_edu = $this->Abiturient->find('count', array('conditions' => array ('Abiturient.id' => $enrolled_ids, 'Abiturient.educationdoctype_id' => $doctypes), 'recursive' => -1,));
						$doctypes = array(3,4);
						$middle_edu = $this->Abiturient->find('count', array('conditions' => array ('Abiturient.id' => $enrolled_ids, 'Abiturient.educationdoctype_id' => $doctypes), 'recursive' => -1,));
						
						//
						/// проверяем подавших заявление
						//
						foreach ($abiturient_ids as $abiturient_id):
						$results = $this->Result->find('all', array('conditions' => array('Result.abiturient_id' => $abiturient_id), 'recursive' => -1, ));
						// проверяем тип оценки
						$is_ege = true;
						foreach ($results as $result):
						if ($result['Result']['resulttype_id'] != 1){
							$is_ege = false;
							break;
						}
						endforeach;
						// проверяем все оценки
						if ($is_ege){
							$p_ege++;
						} else {
							$p_fct++;
						}
						endforeach;
						$doctypes = array(5,6);
						$p_high_edu = $this->Abiturient->find('count', array('conditions' => array ('Abiturient.id' => $abiturient_ids, 'Abiturient.educationdoctype_id' => $doctypes), 'recursive' => -1,));
						$doctypes = array(3,4);
						$p_middle_edu = $this->Abiturient->find('count', array('conditions' => array ('Abiturient.id' => $abiturient_ids, 'Abiturient.educationdoctype_id' => $doctypes), 'recursive' => -1,));
						
						array_push ($spravka, array('facultet_id' => $facultet['Facultet']['id'], 
													 'profession_id' => $profession['Profession']['id'], 
													 'learningform' => $learningform['Learningform']['id'],
													 'learningtype' => $learningtype['Learningtype']['id'],
													 'en_ege' => $ege, 
													 'en_fct' => $fct, 
													 'en_high' => $high_edu, 
													 'en_middle' => $middle_edu,
													 'p_ege' => $p_ege,
													 'p_fct' => $p_fct, 
													 'p_high' => $p_high_edu, 
													 'p_middle' => $p_middle_edu));
					endforeach;
				endforeach;
			endforeach;
		
		endforeach; 
		//debug ($spravka);
		$this->set('spravka', $spravka);		
	/***********************************
	 * 
	 * 
	 **********************************/
		
		$students = $this->Enrolledstudent->find('all', array('conditions' => array('learningtype_id' => 1), 'recursive' => -1,));
		$ids = array();
		foreach ($students as $student):
			array_push($ids, $student['Enrolledstudent']['abiturient_id']);
		endforeach;
		
		$tmp_ids = array();
		foreach ($ids as $tmp_id):
		$results = $this->Result->find('all', array('conditions' => array('Result.abiturient_id' => $tmp_id), 'recursive' => -1, ));
			$is_ege = true;
			foreach ($results as $result):
			if ($result['Result']['resulttype_id'] != 1){
				$is_ege = false;
				break;
			}
			endforeach;		
		if (!$is_ege){
			array_push($tmp_ids, $result['Result']['abiturient_id']);
		}
		endforeach;
			$ids = $tmp_ids;
		
		$b_low_edu = $this->Abiturient->find('count', array('conditions' => array ('Abiturient.id' => $ids, 'Abiturient.schoolgraduated <' => '2009'), 'recursive' => -1,));
		$doctypes = array(5,6);
		$b_high_edu = $this->Abiturient->find('count', array('conditions' => array ('Abiturient.id' => $ids, 'Abiturient.educationdoctype_id' => $doctypes), 'recursive' => -1,));
		$doctypes = array(3,4);
		$b_middle_edu = $this->Abiturient->find('count', array('conditions' => array ('Abiturient.id' => $ids, 'Abiturient.educationdoctype_id' => $doctypes), 'recursive' => -1,));		
		
		$this->set('b_low_edu', $b_low_edu);
		$this->set('b_high_edu', $b_high_edu);
		$this->set('b_middle_edu', $b_middle_edu);
		
		$students = $this->Enrolledstudent->find('all', array('conditions' => array('learningtype_id' => 2), 'recursive' => -1,));
		$vbids = array();
		foreach ($students as $student):
		array_push($vbids, $student['Enrolledstudent']['abiturient_id']);
		endforeach;

		$tmp_ids = array();
		foreach ($vbids as $tmp_id):
		$results = $this->Result->find('all', array('conditions' => array('Result.abiturient_id' => $tmp_id), 'recursive' => -1, ));
		$is_ege = true;
		foreach ($results as $result):
		if ($result['Result']['resulttype_id'] != 1){
			$is_ege = false;
			break;
		}
		endforeach;
		if (!$is_ege){
			array_push($tmp_ids, $result['Result']['abiturient_id']);
		}
		endforeach;
		$vbids = $tmp_ids;
		
		$vb_low_edu = $this->Abiturient->find('count', array('conditions' => array ('Abiturient.id' => $vbids, 'Abiturient.schoolgraduated <' => '2009'), 'recursive' => -1,));
		
		$doctypes = array(5,6);
		$vb_high_edu = $this->Abiturient->find('count', array('conditions' => array ('Abiturient.id' => $vbids, 'Abiturient.educationdoctype_id' => $doctypes), 'recursive' => -1,));
		$doctypes = array(3,4);
		$vb_middle_edu = $this->Abiturient->find('count', array('conditions' => array ('Abiturient.id' => $vbids, 'Abiturient.educationdoctype_id' => $doctypes), 'recursive' => -1,));

		$this->set('vb_low_edu', $vb_low_edu);
		$this->set('vb_high_edu', $vb_high_edu);
		$this->set('vb_middle_edu', $vb_middle_edu);		
		
		
		/*
		 * // подано бюджет
		 */
		
		$abiturient_ids = array();
		$profession_abiturient_ids = $this->Profession->Selectedprofession->find('all', array('conditions' => array('Selectedprofession.learningtype_id' => 1), 'recursive' => -1, 'fields' => array('Selectedprofession.abiturient_id')));
		foreach ($profession_abiturient_ids as $profession_abiturient_id):
		array_push($abiturient_ids, $profession_abiturient_id['Selectedprofession']['abiturient_id']);
		endforeach;

		$tmp_ids = array();
		foreach ($abiturient_ids as $tmp_id):
		$results = $this->Result->find('all', array('conditions' => array('Result.abiturient_id' => $tmp_id), 'recursive' => -1, ));
		$is_ege = true;
			foreach ($results as $result):
			if ($result['Result']['resulttype_id'] != 1){
				$is_ege = false;
				 break;
			}
			endforeach;
		if (!$is_ege){
			array_push($tmp_ids, $result['Result']['abiturient_id']);
		}
		endforeach;
		$abiturient_ids = $tmp_ids;
		

		
		//$p_b_low_edu = $this->Abiturient->find('count', array('conditions' => array ('Abiturient.id' => $abiturient_ids, 'Abiturient.schoolgraduated <' => '2009'), 'recursive' => -1,));
	//	$doctypes = array(5,6);
	//	$p_b_high_edu = $this->Abiturient->find('count', array('conditions' => array ('Abiturient.id' => $abiturient_ids, 'Abiturient.educationdoctype_id' => $doctypes), 'recursive' => -1,));
	//	$doctypes = array(3,4);
	//	$p_b_middle_edu = $this->Abiturient->find('count', array('conditions' => array ('Abiturient.id' => $abiturient_ids, 'Abiturient.educationdoctype_id' => $doctypes), 'recursive' => -1,));

/* 		$this->set('p_b_low_edu', $p_b_low_edu);
		$this->set('p_b_high_edu', $p_b_high_edu);
		$this->set('p_b_middle_edu', $p_b_middle_edu);	 */
		
		$zzz = $abiturient_ids;
		
		$tmp_abi_ids = $this->Abiturient->find('all', array('conditions' => array ('Abiturient.id' => $zzz, 'Abiturient.schoolgraduated <' => '2009'), 'recursive' => -1, 'fields' => array('Abiturient.id')));
		//debug($tmp_abi_ids);
		
		$tmp_ids = array();
		foreach ($tmp_abi_ids as $tmp_abi_id):
		array_push($tmp_ids, $tmp_abi_id['Abiturient']['id']);
		endforeach;
		
		$abiturient_ids = $tmp_ids;
		
		$p_b_low_edu = $this->Profession->Selectedprofession->find('count', array('conditions' => array ('Selectedprofession.abiturient_id' => $abiturient_ids), 'recursive' => -1,));
		$this->set('p_b_low_edu', $p_b_low_edu);
		
		$neyavka2009 = 0;
		
		foreach ($abiturient_ids as $tmp_id):
		$results = $this->Result->find('all', array('conditions' => array('Result.abiturient_id' => $tmp_id), 'recursive' => -1, ));
		$is_ege = true;
		$i = 0 ;
		$k = 0;
		foreach ($results as $result):
		$i++;
			if (($result['Result']['resulttype_id'] == 2)&& ($result['Result']['score'] == 0)){
			$k++;	
			//	break;
			}	
			

		endforeach;
		if ($i== $k){
		$neyavka2009++;}
		endforeach;		
		$this->set('neyavka2009', $neyavka2009);
		

		$doctypes = array(3,4);
		$tmp_abi_ids = $this->Abiturient->find('all', array('conditions' => array ('Abiturient.id' => $zzz, 'Abiturient.educationdoctype_id' => $doctypes), 'recursive' => -1, 'fields' => array('Abiturient.id')));
		//debug($tmp_abi_ids);
		
		$tmp_ids = array();
		foreach ($tmp_abi_ids as $tmp_abi_id):
		array_push($tmp_ids, $tmp_abi_id['Abiturient']['id']);
		endforeach;
		
		$abiturient_ids = $tmp_ids;
		
		$p_b_middle_edu = $this->Profession->Selectedprofession->find('count', array('conditions' => array ('Selectedprofession.abiturient_id' => $abiturient_ids), 'recursive' => -1,));
		$this->set('p_b_middle_edu', $p_b_middle_edu);
		
		$neyavka_middle_edu = 0;
		
		foreach ($abiturient_ids as $tmp_id):
		$results = $this->Result->find('all', array('conditions' => array('Result.abiturient_id' => $tmp_id), 'recursive' => -1, ));
		$is_ege = true;
		$i = 0 ;
		$k = 0;
		foreach ($results as $result):
		$i++;
		if (($result['Result']['resulttype_id'] == 2)&&($result['Result']['score'] == 0)){
			//$is_ege = false;
			$k++;
			//break;
		}	
		endforeach;
		if ($i == $k){
		$neyavka_middle_edu++;}
		endforeach;
		$this->set('neyavka_middle_edu', $neyavka_middle_edu);		
		
		
		
		/*
		 *  внебюджет
		 */
		

		
		$abiturient_ids = array();
		$profession_abiturient_ids = $this->Profession->Selectedprofession->find('all', array('conditions' => array('Selectedprofession.learningtype_id' => 2), 'recursive' => -1, 'fields' => array('Selectedprofession.abiturient_id')));
		foreach ($profession_abiturient_ids as $profession_abiturient_id):
		array_push($abiturient_ids, $profession_abiturient_id['Selectedprofession']['abiturient_id']);
		endforeach;

		$tmp_ids = array();
		foreach ($abiturient_ids as $tmp_id):
		$results = $this->Result->find('all', array('conditions' => array('Result.abiturient_id' => $tmp_id), 'recursive' => -1, ));
		$is_ege = true;
		foreach ($results as $result):
		if ($result['Result']['resulttype_id'] != 1){
			$is_ege = false;
			break;
			
		}
		endforeach;
		if (!$is_ege){
			array_push($tmp_ids, $result['Result']['abiturient_id']);
		}
		endforeach;
		$abiturient_ids = $tmp_ids;
		
/* 		$p_vb_low_edu = $this->Abiturient->find('count', array('conditions' => array ('Abiturient.id' => $abiturient_ids, 'Abiturient.schoolgraduated <' => '2009'), 'recursive' => -1,));
		$doctypes = array(5,6);
		$p_vb_high_edu = $this->Abiturient->find('count', array('conditions' => array ('Abiturient.id' => $abiturient_ids, 'Abiturient.educationdoctype_id' => $doctypes), 'recursive' => -1,));
		$doctypes = array(3,4);
		$p_vb_middle_edu = $this->Abiturient->find('count', array('conditions' => array ('Abiturient.id' => $abiturient_ids, 'Abiturient.educationdoctype_id' => $doctypes), 'recursive' => -1,));

		$this->set('p_vb_low_edu', $p_vb_low_edu);
		$this->set('p_vb_high_edu', $p_vb_high_edu);
		$this->set('p_vb_middle_edu', $p_vb_middle_edu); */		
		

	$zzz = $abiturient_ids;
		
		$tmp_abi_ids = $this->Abiturient->find('all', array('conditions' => array ('Abiturient.id' => $zzz, 'Abiturient.schoolgraduated <' => '2009'), 'recursive' => -1, 'fields' => array('Abiturient.id')));
		//debug($tmp_abi_ids);
		
		$tmp_ids = array();
		foreach ($tmp_abi_ids as $tmp_abi_id):
		array_push($tmp_ids, $tmp_abi_id['Abiturient']['id']);
		endforeach;
		
		$abiturient_ids = $tmp_ids;
		
		$p_vb_low_edu = $this->Profession->Selectedprofession->find('count', array('conditions' => array ('Selectedprofession.abiturient_id' => $abiturient_ids), 'recursive' => -1,));
		$this->set('p_vb_low_edu', $p_vb_low_edu);
		
		$vbneyavka2009 = 0;
		
		foreach ($abiturient_ids as $tmp_id):
		$results = $this->Result->find('all', array('conditions' => array('Result.abiturient_id' => $tmp_id), 'recursive' => -1, ));
		$is_ege = true;
		$i = 0;
		$k = 0;
		foreach ($results as $result):
		$i++;
			if (($result['Result']['resulttype_id'] == 2)&& ($result['Result']['score'] == 0)){
				///$is_ege = false;
				$k++;
				
			//	break;
			}	
		endforeach;
		if ($i == $k) {$vbneyavka2009++;}
		endforeach;		
		$this->set('vbneyavka2009', $vbneyavka2009);
		

		$doctypes = array(3,4);
		$tmp_abi_ids = $this->Abiturient->find('all', array('conditions' => array ('Abiturient.id' => $zzz, 'Abiturient.educationdoctype_id' => $doctypes), 'recursive' => -1, 'fields' => array('Abiturient.id')));
		//debug($tmp_abi_ids);
		
		$tmp_ids = array();
		foreach ($tmp_abi_ids as $tmp_abi_id):
		array_push($tmp_ids, $tmp_abi_id['Abiturient']['id']);
		endforeach;
		
		$abiturient_ids = $tmp_ids;
		
		$p_vb_middle_edu = $this->Profession->Selectedprofession->find('count', array('conditions' => array ('Selectedprofession.abiturient_id' => $abiturient_ids), 'recursive' => -1,));
		$this->set('p_vb_middle_edu', $p_vb_middle_edu);
		
		$vbneyavka_middle_edu = 0;
		
		foreach ($abiturient_ids as $tmp_id):
		$results = $this->Result->find('all', array('conditions' => array('Result.abiturient_id' => $tmp_id), 'recursive' => -1, ));
		$is_ege = true;
		$i = 0;
		$k = 0;
		foreach ($results as $result):
		$i++;
		if (($result['Result']['resulttype_id'] == 2)&&($result['Result']['score'] == 0)){
			$k++;
		//	$is_ege = false;
			
		//	break;
		}	
		endforeach;
		if ($i == $k ){$vbneyavka_middle_edu++;}
		endforeach;
		$this->set('vbneyavka_middle_edu', $vbneyavka_middle_edu);		
		
		
		$doctypes = array(5,6);
		$tmp_abi_ids = $this->Abiturient->find('all', array('conditions' => array ('Abiturient.id' => $zzz, 'Abiturient.educationdoctype_id' => $doctypes), 'recursive' => -1, 'fields' => array('Abiturient.id')));
		//debug($tmp_abi_ids);
		
		$tmp_ids = array();
		foreach ($tmp_abi_ids as $tmp_abi_id):
		array_push($tmp_ids, $tmp_abi_id['Abiturient']['id']);
		endforeach;
		
		$abiturient_ids = $tmp_ids;
		
		$p_vb_high_edu = $this->Profession->Selectedprofession->find('count', array('conditions' => array ('Selectedprofession.abiturient_id' => $abiturient_ids), 'recursive' => -1,));
		$this->set('p_vb_high_edu', $p_vb_high_edu);
		
		$this->render('/','blank','megaspravka');
	}
	
	/***********************************************************************************************
	 * 					МЕГАСПРАВКА 2
	 * 
	 ***********************************************************************************************/
	function megaspravka2() {
		$this->loadModel('Selectedprofession');
		$this->loadModel('Abiturient');
		$this->loadModel('Enrolledstudent');
		// ***************************************************
		// считаем колво поданных заявлений на бакалавриат
		// ***************************************************
		$bakalavriat = array(0);
		$spechialitet = array(10,11,12,13);
 		$selectedprofessions = $this->Selectedprofession->find('all', array('conditions' => array('Selectedprofession.profession_id' => $bakalavriat, 'Selectedprofession.learningform_id' => 1), 'recursive' => -1));
		//debug($selectedprofessions);
		
		// выбираем id абитуриентов
		$abiturient_ids = array();
		foreach ($selectedprofessions as $selectedprofession):
		array_push($abiturient_ids, $selectedprofession['Selectedprofession']['abiturient_id']);
		endforeach; 
		//debug ($abiturient_ids);
		$s023 = 0;
		$s033 = 0;
		$s073 = 0;
		$s083 = 0;
		$s093 = 0;
		$s103 = 0;
		$s133 = 0;
		$s163 = 0;
		$s024 = 0;
		$s034 = 0;
		$s074 = 0;
		$s084 = 0;
		$s094 = 0;
		$s104 = 0;
		$s134 = 0;
		$s164 = 0;		

		$s025 = 0;
		$s035 = 0;
		$s075 = 0;
		$s085 = 0;
		$s095 = 0;
		$s105 = 0;
		$s135 = 0;
		$s165 = 0;
		$s026 = 0;
		$s036 = 0;
		$s076 = 0;
		$s086 = 0;
		$s096 = 0;
		$s106 = 0;
		$s136 = 0;
		$s166 = 0;		
		$s027 = 0;
		$s037 = 0;
		$s077 = 0;
		$s087 = 0;
		$s097 = 0;
		$s107 = 0;
		$s137 = 0;
		$s167 = 0;		
		$s028 = 0;
		$s038 = 0;
		$s078 = 0;
		$s088 = 0;
		$s098 = 0;
		$s108 = 0;
		$s138 = 0;
		$s168 = 0;
		
		$srednee_obr = array(3,4);
		$high_obr = array(5,6);
 		foreach ($abiturient_ids as $abiturient_id):
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => 1)));
		$s023 += $abiturient;
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.schoolgraduated' => 2011)));
		$s033 += $abiturient;
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => 2)));
		$s073 += $abiturient;
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => 2, 'Abiturient.educationdocdata' => '%2011%')));
		$s083 += $abiturient;
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => $srednee_obr)));
		$s093 += $abiturient;
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => $srednee_obr, 'Abiturient.educationdocdata' => '%2011%')));
		$s103 += $abiturient;
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => $high_obr)));
		$s133 += $abiturient;
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => $high_obr, 'Abiturient.educationdocdata' => '%2011%')));
		$s163 += $abiturient;
		endforeach; 
		
		// ***************************************************
		// считаем колво поданных заявлений на специалитет
		// ***************************************************
		//
		$selectedprofessions = $this->Selectedprofession->find('all', array('conditions' => array('Selectedprofession.profession_id' => $spechialitet, 'Selectedprofession.learningform_id' => 1), 'recursive' => -1));
		//debug($selectedprofessions);
		
		// выбираем id абитуриентов
		$abiturient_ids = array();
		foreach ($selectedprofessions as $selectedprofession):
		array_push($abiturient_ids, $selectedprofession['Selectedprofession']['abiturient_id']);
		endforeach;
		//debug ($abiturient_ids);

		
		$srednee_obr = array(3,4);
		$high_obr = array(5,6);
		foreach ($abiturient_ids as $abiturient_id):
 		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => 1)));
		$s026 += $abiturient; 
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => 1, 'Abiturient.schoolgraduated' => 2011)));
		$s036 += $abiturient;
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => 2)));
		$s076 += $abiturient;
 		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => 2, 'Abiturient.educationdocdata' => '%2011%')));
		$s086 += $abiturient;
	$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => $srednee_obr)));
		$s096 += $abiturient;
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => $srednee_obr, 'Abiturient.educationdocdata' => '%2011%')));
		$s106 += $abiturient;
			$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => $high_obr)));
		$s136 += $abiturient;
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => $high_obr, 'Abiturient.educationdocdata' => '%2011%')));
		$s166 += $abiturient; 
		endforeach; 
		
 		// ***************************************************
		// считаем колво зачисленных на бакалавриат всего
		// ***************************************************
		$enrolledstudents = $this->Enrolledstudent->find('all', array('conditions' => array('Enrolledstudent.profession_id' => $bakalavriat, 'Enrolledstudent.learningform_id' => 1)));
		// выбираем id абитуриентов
		$abiturient_ids = array();
		foreach ($enrolledstudents as $enrolledstudent):
		array_push($abiturient_ids, $enrolledstudent['Enrolledstudent']['abiturient_id']);
		endforeach;
		

		
		$srednee_obr = array(3,4);
		$high_obr = array(5,6);
		foreach ($abiturient_ids as $abiturient_id):
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => 1)));
		$s024 += $abiturient;
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.schoolgraduated' => 2011)));
		$s034 += $abiturient;
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => 2)));
		$s074 += $abiturient;
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => 2, 'Abiturient.educationdocdata' => '%2011%')));
		$s084 += $abiturient;
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => $srednee_obr)));
		$s094 += $abiturient;
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => $srednee_obr, 'Abiturient.educationdocdata' => '%2011%')));
		$s104 += $abiturient;
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => $high_obr)));
		$s134 += $abiturient;
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => $high_obr, 'Abiturient.educationdocdata' => '%2011%')));
		$s164 += $abiturient;
		
		endforeach; 
		// а теперь внебюджет
		
 		$enrolledstudents = $this->Enrolledstudent->find('all', array('conditions' => array('Enrolledstudent.profession_id' => $bakalavriat, 'Enrolledstudent.learningform_id' => 1, 'Enrolledstudent.learningtype_id' => 2)));
		// выбираем id абитуриентов
		$abiturient_ids = array();
		foreach ($enrolledstudents as $enrolledstudent):
		array_push($abiturient_ids, $enrolledstudent['Enrolledstudent']['abiturient_id']);
		endforeach;
		

		
	$srednee_obr = array(3,4);
		$high_obr = array(5,6);
		foreach ($abiturient_ids as $abiturient_id):
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => 1)));
		$s025 += $abiturient;
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.schoolgraduated' => 2011)));
		$s035 += $abiturient;
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => 2)));
		$s075 += $abiturient;
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => 2, 'Abiturient.educationdocdata' => '%2011%')));
		$s085 += $abiturient;
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => $srednee_obr)));
		$s095 += $abiturient;
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => $srednee_obr, 'Abiturient.educationdocdata' => '%2011%')));
		$s105 += $abiturient;
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => $high_obr)));
		$s135 += $abiturient;
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => $high_obr, 'Abiturient.educationdocdata' => '%2011%')));
		$s165 += $abiturient;
		endforeach; 
		
		// ***************************************************
		// считаем колво зачисленных на специалитет всего
		// ***************************************************
		$enrolledstudents = $this->Enrolledstudent->find('all', array('conditions' => array('Enrolledstudent.profession_id' => $spechialitet, 'Enrolledstudent.learningform_id' => 1)));
		// выбираем id абитуриентов
		$abiturient_ids = array();
		foreach ($enrolledstudents as $enrolledstudent):
		array_push($abiturient_ids, $enrolledstudent['Enrolledstudent']['abiturient_id']);
		endforeach;
		

		
		$srednee_obr = array(3,4);
		$high_obr = array(5,6);
		foreach ($abiturient_ids as $abiturient_id):
 		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => 1)));
		$s027 += $abiturient; 
		 $abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => 1, 'Abiturient.schoolgraduated' => 2011)));
		$s037 += $abiturient; 
	 	$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => 2)));
		$s077 += $abiturient;
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => 2, 'Abiturient.educationdocdata' => '%2011%')));
		$s087 += $abiturient;
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => $srednee_obr)));
		$s097 += $abiturient;
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => $srednee_obr, 'Abiturient.educationdocdata' => '%2011%')));
		$s107 += $abiturient;
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => $high_obr)));
		$s137 += $abiturient;
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => $high_obr, 'Abiturient.educationdocdata' => '%2011%')));
		$s167 += $abiturient; 
		endforeach;  
		
		
		// а теперь внебюджет
		
 		$enrolledstudents = $this->Enrolledstudent->find('all', array('conditions' => array('Enrolledstudent.profession_id' => $spechialitet, 'Enrolledstudent.learningform_id' => 1, 'Enrolledstudent.learningtype_id' => 2)));
		// выбираем id абитуриентов
		$abiturient_ids = array();
		foreach ($enrolledstudents as $enrolledstudent):
		array_push($abiturient_ids, $enrolledstudent['Enrolledstudent']['abiturient_id']);
		endforeach;
	//	debug($abiturient_ids);

		
		$srednee_obr = array(3,4);
		$high_obr = array(5,6);
		foreach ($abiturient_ids as $abiturient_id):
 	$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => 1)));
		$s028 += $abiturient; 
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => 1, 'Abiturient.schoolgraduated' => 2011)));
		$s038 += $abiturient; 
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => 2)));
		$s078 += $abiturient;
	 	$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => 2, 'Abiturient.educationdocdata' => '%2011%')));
		$s088 += $abiturient;
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => $srednee_obr)));
		$s098 += $abiturient;
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => $srednee_obr, 'Abiturient.educationdocdata' => '%2011%')));
		$s108 += $abiturient;
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => $high_obr)));
		$s138 += $abiturient;
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id, 'Abiturient.educationdoctype_id' => $high_obr, 'Abiturient.educationdocdata' => '%2011%')));
		$s168 += $abiturient;  
		endforeach;
		//debug ($s023);
		$this->set('s023', $s023);
		$this->set('s033', $s033);
		$this->set('s073', $s073);
		$this->set('s083', $s083);
		$this->set('s093', $s093);
		$this->set('s103', $s103);
		$this->set('s133', $s133);
		$this->set('s163', $s163);
		
		$this->set('s024', $s024);
		$this->set('s034', $s034);
		$this->set('s074', $s074);
		$this->set('s084', $s084); 
		$this->set('s094', $s094);
		$this->set('s104', $s104);
		$this->set('s134', $s134);
		$this->set('s164', $s164);	
		
		$this->set('s025', $s025);
		$this->set('s035', $s035);
		$this->set('s075', $s075);
		$this->set('s085', $s085);
		$this->set('s095', $s095);
		$this->set('s105', $s105);
		$this->set('s135', $s135);
		$this->set('s165', $s165);
		
					
		$this->set('s026', $s026);
		$this->set('s036', $s036);		
		$this->set('s076', $s076);		
		$this->set('s086', $s086);		
		$this->set('s096', $s096);		
		$this->set('s106', $s106);		
		$this->set('s136', $s136);
		$this->set('s166', $s166);
		
		$this->set('s027', $s027);
		$this->set('s037', $s037);
		$this->set('s077', $s077);
		$this->set('s087', $s087);
		$this->set('s097', $s097);
		$this->set('s107', $s107);
		$this->set('s137', $s137);
		$this->set('s167', $s167);
		
		$this->set('s028', $s028);
		$this->set('s038', $s038);
		$this->set('s078', $s078);
		$this->set('s088', $s088);
		$this->set('s098', $s098);
		$this->set('s108', $s108);
		$this->set('s138', $s138);
		$this->set('s168', $s168);
		
/* 		$this->set('s023, s033, s073, s083, s093, s103, s133, s163, 
 * s024, s034, s074, s084, s094, s104, s134, s164, s025, 
 * s035, s075, s085, s095, s105, s135, s165,	s026, s036, s076, s086, s096, s106, s136, s166,	s027, s037, s077, s087, s097, s107, s137, s167,	s028, s038, s078, s088, s098, s108, s138, s168', 
		$s023, $s033, $s073, $s083, $s093, $s103, $s133, $s163,
		$s024, $s034, $s074, $s084, $s094, $s104, $s134, $s164,
		$s025, $s035, $s075, $s085, $s095, $s105, $s135, $s165,
		$s026, $s036, $s076, $s086, $s096, $s106, $s136, $s166,
		$s027, $s037, $s077, $s087, $s097, $s107, $s137, $s167,
		$s028, $s038, $s078, $s088, $s098, $s108, $s138, $s168
		); */
		
		$this->render('/','blank','megaspravka2');
		
	}
	
	
	
	
	
	function megaspravka3 (){
		$this->loadModel('Selectedprofession');
		$this->loadModel('Abiturient');
		$this->loadModel('Enrolledstudent');
		// dscitt образование
		// ***************************************************
		// считаем колво поданных заявлений на бакалавриат
		// ***************************************************
		$bakalavriat = array(6,8,9,15);// заочно 6
		//$spechialitet = array(1,2,3,4,5,7); // заочно 5 
		$spechialitet = array(10,11,12,13); // заочно 5
		$s013=0;		$s023=0;		$s033=0;		$s043=0;		$s053=0;		$s083=0;	$s103=0;	$s113=0;		$s123=0; $s133=0;
		$s014=0;		$s024=0;		$s034=0;		$s044=0;		$s054=0;		$s084=0;	$s104=0;	$s114=0;		$s124=0; $s134=0;
		$s015=0;		$s025=0;		$s035=0;		$s045=0;		$s055=0;		$s085=0;	$s105=0;	$s115=0;		$s125=0; $s135=0;
		$s016=0;		$s026=0;		$s036=0;		$s046=0;		$s056=0;		$s086=0;	$s106=0;	$s116=0;		$s126=0; $s136=0;
		$s017=0;		$s027=0;		$s037=0;		$s047=0;		$s057=0;		$s087=0;	$s107=0;	$s117=0;		$s127=0; $s137=0;
		$s018=0;		$s028=0;		$s038=0;		$s048=0;		$s058=0;		$s088=0;	$s108=0;	$s118=0;		$s128=0; $s138=0;
		// бакалавриат
	//	$selectedprofessions = $this->Selectedprofession->find('all', array('conditions' => array('Selectedprofession.profession_id' => $spechialitet, 'Selectedprofession.learningform_id' => 1), 'recursive' => -1));
		$selectedprofessions = $this->Enrolledstudent->find('all', array('conditions' => array('Enrolledstudent.profession_id' => $spechialitet, 'Enrolledstudent.learningform_id' => 1, 'Enrolledstudent.learningtype_id' => 2)));
		// выбираем id абитуриентов
		$abiturient_ids = array();

		foreach ($selectedprofessions as $selectedprofession):
		array_push($abiturient_ids, $selectedprofession['Enrolledstudent']['abiturient_id']);

		endforeach;	

		// считаем колво поданных на бакалавриат всего
		foreach ($abiturient_ids as $abiturient_id):
		$abiturient = $this->Abiturient->find('count', array('conditions' => array('Abiturient.id' => $abiturient_id)));
		$s013 += $abiturient;
		endforeach; 
		$this->set('s013', $s013);
		foreach ($abiturient_ids as $abiturient_id):
		$results = $this->Abiturient->Result->find('all', array('conditions' => array('Result.abiturient_id' => $abiturient_id), 'recursive' => -1));
			$result_count = 0;
			$i=0;
			foreach ($results as $result):
				$i++;
				if ($result['Result']['resulttype_id'] == 1) {
					$result_count++;
				}
			endforeach;
			if ($i == $result_count){
				$s033++;
			}
			if ($result_count == 0) {
				$s053++;
			}
			if (($result_count !=0)&& ($result_count != $i)){
				$s043++;
			}
		endforeach;
		$this->set('s033', $s033);
		$this->set('s043', $s043);
		$this->set('s053', $s053);
		//$j=0;
		foreach ($abiturient_ids as $abiturient_id):
		//$j++;
		$abiturients = $this->Abiturient->find('all', array('conditions' =>array('Abiturient.id' => $abiturient_id), 'fields' => array('Abiturient.id')));
				if (!$abiturients[0]['Privilege']){
					$s083++;
					
				} else {
				//	debug ($abiturients[0]);
					if ($abiturients[0]['Privilege'][0]['AbiturientsPrivilege']['privilege_id'] == 1){
						$s113++;
					} else {
						$s103++;
						$s123++;
					}
				}
				if ($abiturients[0]['Prioritet']){
					$s133++;
				}
		endforeach;
	//	debug($j);
		$this->set('s083', $s083);		
		$this->set('s103', $s103);
		$this->set('s113', $s113);
		$this->set('s123', $s123);
		$this->set('s133', $s133);



	}
}	
?>